package com.feng.service.impl;

import com.feng.mapper.DeptLogMapper;
import com.feng.mapper.DeptMapper;
import com.feng.mapper.EmpMapper;
import com.feng.pojo.Dept;
import com.feng.pojo.DeptLog;
import com.feng.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptLogMapper deptLogMapper;
    @Override
    public List<Dept> list() {
        List<Dept> list = deptMapper.list();
        return list;
    }
    //根据id删除
    @Transactional
    @Override
    public void delete(Integer id) {
        try {
            deptMapper.delete(id);//根据id删除
            empMapper.deleteByDeptId(id);//根据部门工D删除该部门下的员工
        } finally {
            DeptLog deptLong = new DeptLog();
            deptLong.setCreateTime(LocalDateTime.now());
            deptLong.setDescription("执行了解散部门的操作，此次解散的是"+id+"号部门");
            deptLogMapper.insert(deptLong);
        }
    }
    //新增部门
    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }
    //根据id查询
    @Override
    public Dept selectById(Integer id) {
        Dept dept =deptMapper.selectById(id);
        return dept;
    }
    //修改部门
    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
